import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
import 'package:liveplugin/type.dart';

/**
 *
 * @ProjectName:    live_plugin
 * @ClassName:      LivePushControl
 * @Description:    预览控制器
 * @Author:         孙浩
 * @QQ:             243280864
 * @CreateDate:     2020/4/9 14:01
 */
class LivePushControl {
  MethodChannel _methodChannel =
      MethodChannel("com.sunstar.liveplugin.viewControl");
  EventChannel _eventChannel =
      EventChannel("com.sunstar.liveplugin.viewControl_event");

  ///添加监听
  void setOnLiveListener(ValueChanged<int> listener) {
    _eventChannel.receiveBroadcastStream().listen((data) {
      listener.call(data["type"]);
      print(data["info"]);
    });
  }

  //该方法iOS端要先于其它所有插件内方法调用，安卓端不知道
  //isVertical:是否竖屏，默认为true
  void initMediaCapture({bool isVertical = true}){
    _methodChannel.invokeMapMethod("initMediaCapture", {"isVertical": isVertical});
  }

  ///开始预览
  void startPreview({bool isFaceU = true}) {
    _methodChannel.invokeMapMethod("preView", {"isFaceU": isFaceU});
  }

  ///销毁预览
  destroy() async {
    return _methodChannel.invokeMapMethod("destroy");
  }

  ///切换摄像头
  void switchCamera() {
    _methodChannel.invokeMapMethod("switchCamera");
  }

  ///修改美颜效果
  void setBeauty(
      {double whiteColor = 0.5,
      double redColor = 0.5,
      double blur = 0.5,
      filterName = FilterData.EMPTY,
      double filterValue = 0.5}) {
    _methodChannel.invokeMapMethod("initBeauty", {
      "whiteColor": whiteColor,
      "redColor": redColor,
      "blur": blur,
      "filterName": filterName,
      "filterValue": filterValue
    });
  }

  ///修改滤镜
  void changeFilterType(String name) {
    FilterType type;
    switch (name) {
      case "无":
        type = FilterType.none;
        break;
      case "干净":
        type = FilterType.clean;
        break;
      case "童话":
        type = FilterType.fairytale;
        break;
      case "自然":
        type = FilterType.nature;
        break;
      case "健康":
        type = FilterType.healthy;
        break;
      case "温柔":
        type = FilterType.tender;
        break;
      case "美白":
        type = FilterType.whiten;
        break;
    }
    _methodChannel
        .invokeMapMethod("changeFilter", {"filterType": type.toString()});
  }

  void pushVideoStreaming(String url) =>
      _methodChannel.invokeMethod("startPushStreaming", {"pushUrl": url});

  ///修改滤镜等级
  void changeFilterLevel(double value) {
    _methodChannel.invokeMapMethod("changeFilterLevel", {"level": value});
  }

  ///修改美颜等级
  void changeBeauty(double value) {
    _methodChannel.invokeMapMethod("changeBeautyLevel", {"level": value});
  }

  ///添加水印月野兔样式
  void addWaterMarker() {
    _methodChannel.invokeMapMethod("addWaterMarker", {"x": 100, "y": 30});
  }

  ///开启背景音乐
  void startMusicPlay(String urlPath, bool isLoop) {
    _methodChannel.invokeMapMethod(
        "startPlayMusic", {"urlPath": urlPath, "isLoop": isLoop});
  }

  void pausePushVideo(){
    _methodChannel.invokeMethod("pauseLiveStream");
  }

  void rePushVideo(){
    _methodChannel.invokeMethod("rePushLiveStream");
  }

  void changeOrigation({bool isVertical}) {
    if (isVertical != null) {
      _methodChannel.invokeMapMethod(
          "changeOrigation", {"isVertical": isVertical});
    }else{
      _methodChannel.invokeMapMethod(
          "changeOrigation");
    }
  }

  void auth() {
    ByteData byte = ByteData(1150);
  }
}
